package cn.jhc.sevlet;//修改成绩

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.Clazz;
import cn.jhc.bean.Course;
import cn.jhc.bean.Score;
import cn.jhc.bean.Student;
import cn.jhc.dao.ClassDao;
import cn.jhc.dao.CourseDao;
import cn.jhc.dao.ScoreDao;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.impl.ClassDaoImpl;
import cn.jhc.dao.impl.CourseDaoImpl;
import cn.jhc.dao.impl.ScoreDaoImp;
import cn.jhc.dao.impl.StudentDaoImpl;

/**
 * Servlet implementation class AlterscoreServlet
 */
@WebServlet("/user/teacher/alterscore.do")
public class AlterscoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AlterscoreServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		int scoreid = Integer.valueOf(request.getParameter("scoreid"));
		String sno = request.getParameter("sno");
		StudentDao studentDao = new StudentDaoImpl();
		Student student = studentDao.getStudent(sno);
		String coursename = request.getParameter("coursename");
//		System.out.println(coursename+"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
		CourseDao courseDao = new CourseDaoImpl();
		List<Course> courses = courseDao.list();
		Course course = null;
		for(int i=0;i<=courses.size();i++){
			if(courses.get(i).getCoursename().equals(coursename)){
				course = courses.get(i);
				break;
			}
		}
		BigDecimal uscore = new BigDecimal(request.getParameter("uscore"));
		BigDecimal endscore = new BigDecimal(request.getParameter("endscore"));
		BigDecimal totalscore = new BigDecimal(request.getParameter("totalscore"));
		String levelscore = request.getParameter("levelscore");
		String tname = request.getParameter("tname");
		String term = request.getParameter("term");
		ClassDao classDao = new ClassDaoImpl();
		String clazzname = request.getParameter("clazzname");
		List<Clazz> clazzs = classDao.list(clazzname);
		System.out.println(clazzs.size()+clazzname+sno+coursename+"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
		Clazz clazz = clazzs.get(0);
		String college = request.getParameter("college");
		Score score = new Score();
		score.setScoreid(scoreid);
		score.setStudent(student);
		score.setUscore(uscore);
		score.setEndscore(endscore);
		score.setTotalscore(totalscore);
		score.setLevelscore(levelscore);
		score.setCourse(course);
		System.out.println("save!!!!!!!!!!!!!!!!!!!!!");
		ScoreDao scoreDao = new ScoreDaoImp();
		scoreDao.update(score);
		response.sendRedirect("./tscore.do");
	}

}
